/*pkwSUp3YDQgoGR3wOUJFGpIk4Ijp1j2oOhZH+zqry7MQtWLWwkiORnpHaaKijnTs*/
/*
 * @Descripttion:
 * @version:
 * @Author: Chendch
 * @Date: 2019-06-08 15:55:18
 * @LastEditors: Chendch
 * @LastEditTime: 2021-06-10 16:01:16
 */
/**
 * @desc 数字加千分位功能（将阿拉伯数字每3位1逗号分隔）
 * @param  {str}
 * @return {str}
 */
export const cutStr = (str) => {
    let result = [ ], counter = 0;
    let num = (num || 0).toString().split('');
    for (let i = num.length - 1; i >= 0; i--) {
        counter++;
        result.unshift(num[i]);
        if (!(counter % 3) && i != 0) { result.unshift(','); }
    }
    return result.join('');
}

/**
 * 数字金额大写转换(可以处理整数,小数,负数)
 * @param {Number} n 想要转换的数字
 * @return  {String}
 */
export const smalltoBIG = (n = 0) => {
    var fraction = ['角', '分'];
    var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
    var unit = [ ['元', '万', '亿','兆'], ['', '拾', '佰', '仟','万']  ];
    var head = n < 0? '欠': '';
    n = Math.abs(n);

    var s = '';

    for (var i = 0; i < fraction.length; i++)
    {
      s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
    }
    s = s || '整';
    n = Math.floor(n);

    for (var i = 0; i < unit[0].length && n > 0; i++)
    {
      var p = '';
      for (var j = 0; j < unit[1].length && n > 0; j++)
      {
        p = digit[n % 10] + unit[1][j] + p;
        n = Math.floor(n / 10);
      }
      s = p.replace(/(零.)*零$/, '').replace(/^$/, '零')  + unit[0][i] + s;
    }
    return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
  }

// 将科学计数法转成数字字符串
export function convertNum(num_str) {
    //参数必须为 字符串
    //科学计数法字符 转换 为数字字符， 突破正数21位和负数7位的Number自动转换
    // 兼容 小数点左边有多位数的情况，即 a×10^b（aEb），a非标准范围（1≤|a|<10）下的情况。如 3453.54E-6 or 3453.54E6
    let resValue = '',
      power = '',
      result = null,
      dotIndex = 0,
      resArr = [],
      sym = '';
    let numStr = String(num_str);
    if(!/e/i.test(numStr)) {
      return numStr;
    }
    if (numStr[0] == '-') {
      // 如果为负数，转成正数处理，先去掉‘-’号，并保存‘-’.
      numStr = numStr.substr(1);
      sym = '-';
    }
    let regExp = new RegExp('^(((\\d+.?\\d+)|(\\d+))[Ee]{1}((-(\\d+))|(\\d+)))$', 'ig');
    result = regExp.exec(numStr);
    if (result != null) {
      resValue = result[2];
      power = result[5];
      result = null;
    }
    if (!resValue && !power) {
      return false;
    }
    dotIndex = resValue.indexOf('.');
    resValue = resValue.replace('.', '');
    resArr = resValue.split('');
    if (Number(power) >= 0) {
      let subres = resValue.substr(dotIndex);
      power = Number(power);
      //幂数大于小数点后面的数字位数时，后面加0
      for (let i = 0; i < power - subres.length; i++) {
          resArr.push('0');
      }
      if (power - subres.length < 0) {
          resArr.splice(dotIndex + power, 0, '.');
      }
    } else {
      power = power.replace('-', '');
      power = Number(power);
      //幂数大于等于 小数点的index位置, 前面加0
      for (let i = 0; i <= power - 1; i++) {
          resArr.unshift('0');
      }
      let n = power - dotIndex >= 0 ? 1 : -(power - dotIndex);
      resArr.splice(n, 0, '.');
    }
    resValue = resArr.join('');
    return sym + resValue;
}
//数字转换成千分位 格式
export function commafy(num) {
    let pointIndex, intPart, pointPart;
    if (num === '-') {
        return '-';
    }

    if (Number.isNaN(+(num + '').split(',').join(''))) {
        //这里暂时就处理一下千分位的逗号
        return '';
    }

    num = num + '';
    if (/^.*\..*$/.test(num)) {
        pointIndex = num.lastIndexOf('.');
        intPart = num.substring(0, pointIndex);
        pointPart = num.substring(pointIndex + 1, num.length);
        intPart = intPart + '';
        let re = /(-?\d+)(\d{3})/;
        while (re.test(intPart)) {
            intPart = intPart.replace(re, '$1,$2');
        }
        num = intPart + '.' + pointPart;
    } else {
        num = num + '';
        let re = /(-?\d+)(\d{3})/;
        while (re.test(num)) {
            num = num.replace(re, '$1,$2');
        }
    }
    return num;
}

// 精度处理
export function formatDot(value, len = 8) {
    let formatVal, dotSplit, val;

    val = (value || 0).toString();

    dotSplit = val.split('.');

    if (dotSplit.length > 2 || !value) {
        return value;
    }

    if (val.indexOf('.') > -1) {
        if (len == 0) {
            formatVal = dotSplit[0];
        } else {
            formatVal = val.substring(0, val.indexOf('.') + len + 1);
        }
    } else {
        formatVal = val;
    }

    return formatVal;
}
// 补0
export const addZero = (num, scale) => {
    if (num === '' || num === undefined || num === null) {
        return '';
    }

    if (scale > 0) {
        let start = String(num).split('.')[0];
        let end = String(num).split('.')[1];
        if (!end) {
            end = '';
        }
        let len = end.length;
        if (len < scale) {
            end = end.padEnd(scale, '0');
        }
        return start + '.' + end;
    } else {
        return num;
    }
};

//精度 + 补0 + 千分位综合处理
export function formatAcuracy(value, len = 0) {
    if (value === null || value === undefined || String(value).endsWith('必输项')) {
        return value;
    }
    // 将科学计数法转成数字字符串
    value = convertNum(value);
    return commafy(addZero(formatDot(value, len), len));
}

export function numRandom() {
    if(!window.crypto && (window).msCrypto){
        (window).crypto = (window).msCrypto;
    }
    return window.crypto.getRandomValues(new Uint32Array(1))[0] / 4294967296;
}
/*pkwSUp3YDQgoGR3wOUJFGpIk4Ijp1j2oOhZH+zqry7MQtWLWwkiORnpHaaKijnTs*/
